package com.dw.etl.core.controller;

import javax.servlet.http.HttpServletRequest;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.util.CommonStringUtil;
import com.dw.etl.core.bean.vo.LayMsg;
import org.springframework.beans.factory.annotation.Autowired;
import  com.dw.etl.common.base.BaseResult;
import  com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.core.api.MdMedDimService;
import com.dw.etl.core.bean.model.MdMedDim;
import org.springframework.util.StringUtils;
import  com.dw.etl.common.vo.PaginationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * mdMedDim控制器
 * Created by hozeData on 2021/07/05.
 */
@RestController
@RequestMapping("/etl/mdMedDim/")
@Api(tags="维度管理")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdMedDimController {

    @Autowired
    MdMedDimService mdMedDimService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.GET)
    public LayMsg findByPage(String filter, Integer page, Integer limit) {
        PaginationVO<MdMedDim> pageVO = mdMedDimService.findPageByLike(filter, (page-1)*limit, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="mdMedPutfd", value="实体", paramType="body", required=true, dataType="MdMedPutfd")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody MdMedDim mdMedDim, HttpServletRequest request)  {
        int num;
        if(StringUtils.isEmpty(mdMedDim.getIdDim())) {
            String idPubfld = mdMedDim.getIdPubfld();
            mdMedDim.setIdDim(mdMedDim.getIdPubfld());
            idPubfld = idPubfld.substring(idPubfld.indexOf("dim_"));
            mdMedDim.setCd(CommonStringUtil.underlineToCamel(idPubfld));
            num = mdMedDimService.insertSelective(mdMedDim);
        } else {
            num = mdMedDimService.updateByIdSelective(mdMedDim);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="统一生成编码")
    @RequestMapping(value="createCd", method=RequestMethod.GET)
    public BaseResult createCd() {
        List<MdMedDim> list = mdMedDimService.findByModel(new MdMedDim());
        for(MdMedDim mdMedDim:list) {
            String idPubfld = mdMedDim.getIdPubfld();
            mdMedDim.setIdDim(mdMedDim.getIdPubfld());
            idPubfld = idPubfld.substring(idPubfld.indexOf("dim_")+4);
            mdMedDim.setCd(CommonStringUtil.underlineToCamel(idPubfld));
            mdMedDim.setDtSysCre(new Date());
            mdMedDim.setDtSysModi(new Date());
            mdMedDimService.updateByIdSelective(mdMedDim);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, 0);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idDim", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idDim, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDim)){
            throw new BaseException("请选择要编辑的项");
        }
        MdMedDim mdMedDim = mdMedDimService.getById(idDim);
        return new BaseResult(CommonResultConstant.SUCCESS, mdMedDim);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idDim", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idDim, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDim)){
            throw new BaseException("请选择要删除的项");
        }
        int num = mdMedDimService.deleteById(idDim);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
